package com.xvyy.tingshu.cdc.handler;


import com.xvyy.tingshu.cdc.entity.CDCEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import top.javatool.canal.client.annotation.CanalTable;
import top.javatool.canal.client.handler.EntryHandler;

/**
 * ClassName: CDCEntryHandler
 * Package: com.xvyy.tingshu.cdc.handler
 *
 * @Description:
 * @Author: xvyy
 * @Create: 2025/2/9 - 12:31
 * @Version: v1.0
 */
@Component
@CanalTable(value = "album_info")
public class CDCEntryHandler implements EntryHandler<CDCEntity> {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;
/*    @Autowired
    private RabbitService rabbitService;*/

    Logger logger = LoggerFactory.getLogger(CDCEntryHandler.class);

    /**
     * 监听新增数据
     *
     * @param cdcEntity
     */
    @Override
    public void insert(CDCEntity cdcEntity) {
        logger.info("insert: {}", cdcEntity);
    }

    /**
     * 监听更新数据
     *
     * @param before
     * @param after
     */
    @Override
    public void update(CDCEntity before, CDCEntity after) {

//        String cacheKey = RedisConstant.CACHE_INFO_PREFIX + after.getId();
        String cacheKey = "cache:info:" + after.getId();
        try {
            Thread.sleep(500);//模拟业务逻辑处理耗时
            logger.info("修改前的数据：{}", before);
            stringRedisTemplate.delete(cacheKey);
        } catch (InterruptedException e) {
            // 如果有异常，发送缓存key给mq，让其他服务重新拉取数据
//            rabbitService.sendDeadlyMessage(MqConst.EXCHANGE_CANAL, MqConst.ROUTING_CANAL_UPDATE, cacheKey, 10);
        }

        logger.info("修改后的数据{}", after);
    }

    /**
     * 监听删除数据
     *
     * @param cdcEntity
     */
    @Override
    public void delete(CDCEntity cdcEntity) {

        logger.info("delete: {}", cdcEntity);

    }
}
